package com.interauto.service.web;

import com.alibaba.fastjson.JSON;
import com.interauto.Dao.Mapper.InterStInter;
import com.interauto.Dao.Mapper.MgInterinfoInter;
import com.interauto.Dao.entity.Checktable;
import com.interauto.Dao.entity.Interinfo;
import com.interauto.bean.web.AssinfoWeb;
import com.interauto.bean.web.InterinfoWeb;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.interauto.util.AutoStringUtils.getJsontoMap;
import static com.interauto.util.AutoStringUtils.getMaptoJson;

/**
 * 处理接口相关的逻辑service层
 */
@Component
public class InterinfoPageService {
    private static final Logger logger= LoggerFactory.getLogger(InterinfoPageService.class);
    @Autowired
    private MgInterinfoInter mgInterinfoInter;

    @Autowired
    private InterStInter interStInter;


    /**
     * 往数据库里添加要运行的接口
     * @param interinfoWeb 要保存的接口信息
     * @return true=成功 false=失败
     */
    public boolean addinterinfo(InterinfoWeb interinfoWeb){
        if(interinfoWeb == null){
            return false;
        }

        Interinfo interinfo=new Interinfo();
        setinterinfo(interinfoWeb,interinfo);
        try{
            mgInterinfoInter.addinterinfo(interinfo);
            if(interinfo.getId()<1){
                logger.info("addinterinfo is system error;interinfo is-->"+interinfo.toString());
                return false;
            }
            List<AssinfoWeb> assinfoWebs=interinfoWeb.getAssinfoWebs();
            if(assinfoWebs == null || assinfoWebs.isEmpty()){
                logger.info("-->> assinfoWebs is empty");
                return true;
            }
            int interinfoid=interinfo.getId();
            for(AssinfoWeb assinfoWeb:assinfoWebs){
                String checkinfo=assinfoWeb.getCheckinfo();
                if(StringUtils.isEmpty(checkinfo)){
                    //如果该值为空，就不保存该校验点了。前端不好处理的
                    continue;
                }
                Checktable checktable=getchecktableinfo(assinfoWeb,interinfoid);
                if(checktable == null){
                    continue;
                }
                mgInterinfoInter.addcheckinfo(checktable);
            }
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error(String.format("-->>addinterinfo is system error; info:%s,message:%s",interinfoWeb.toString(),e));
        }
        return false;
    }

    /**
     * 修改要跑的接口信息
     * @param interinfoWeb 要修改的信息哈
     */
    public void upinterinfo(InterinfoWeb interinfoWeb){
        if(interinfoWeb == null){
            return;
        }
        Interinfo interinfo=new Interinfo();
        setinterinfo(interinfoWeb,interinfo);
        try{
            mgInterinfoInter.upinterinfo(interinfo);
            int interid=interinfo.getId();
            mgInterinfoInter.delcheckinfotointerid(interid);  //要修改前，都将原有的断言信息，先全删除掉
            List<AssinfoWeb> assinfoWebs=interinfoWeb.getAssinfoWebs();
            if(assinfoWebs == null || assinfoWebs.isEmpty()){
                logger.info("upinterinfo assinfoWebs is empty");
                return;
            }
            for(AssinfoWeb assinfoWeb:assinfoWebs){
                Checktable checktable=getchecktableinfo(assinfoWeb,interid);
                if(checktable == null){
                    continue;
                }
                mgInterinfoInter.addcheckinfo(checktable);
            }
        }
        catch (Exception e){
            logger.error("-->>upinterinfo is system error;"+e);
        }
    }

    /**
     * 删除要跑的接口信息
     * @param interinfoid 要删除的接口id
     */
    public void delinterinfo(int interinfoid){
        try{
            mgInterinfoInter.delinterinfo(interinfoid);
        }
        catch (Exception e){
            logger.error(String.format("delinterinfo is system error; interinfoid:%s,messageinfo:%s",interinfoid,e));
        }
    }

    /**
     * 通过用例ID，找到该用例ID关联的要跑的接口信息
     * @param caseid 要查询的用例ID
     * @return 所关联的接口信息
     */
    public List<InterinfoWeb> getInterinfoforCaseid(int caseid){
        try{
            List<Interinfo> interinfos= interStInter.getInterinfoforCaseid(caseid);
            if(interinfos == null || interinfos.isEmpty()){
                return null;
            }
            List<InterinfoWeb> interinfoWebs=new ArrayList<>();
            for(Interinfo interinfo:interinfos){
                InterinfoWeb interinfoWeb=new InterinfoWeb();
                BeanUtils.copyProperties(interinfo,interinfoWeb);
                interinfoWeb.setInterid(interinfo.getId());
                //interinfoWeb.setHeadstr(getMaptoJson(interinfo.getHeadstr()));
                interinfoWebs.add(interinfoWeb);
            }
            return interinfoWebs;
        }
        catch (Exception e){
            logger.error(String.format("getInterinfoforCaseid is system error;caseid:%s,errormessage:%s",caseid,e));
        }
        return null;
    }


    /**
     * 通过接口ID，查询该接口的详细信息
     * @param interid 要查询的接口id
     */
    public InterinfoWeb getIngerinfforid(int interid){
        try{
            Interinfo interinfo= interStInter.getInterinfoforid(interid);
            if(interinfo == null){
                logger.info("getInterinfoforid is null; interid is:"+interid);
                return null;
            }
            InterinfoWeb interinfoWeb=new InterinfoWeb();
            BeanUtils.copyProperties(interinfo,interinfoWeb);
            interinfoWeb.setHeadstr(getMaptoJson(interinfo.getHeadstr()));
            interinfoWeb.setInterid(interinfo.getId());
            List<Checktable> checktableList=interStInter.getChecklistforinterid(interinfo.getId());
            if(checktableList == null || checktableList.isEmpty()){
                logger.info(String.format("getChecklistforinterid is empty,interid is:%s",interid));
                return interinfoWeb;
            }
            List<AssinfoWeb> assinfoWebList=new ArrayList<>();
            for(Checktable checktable:checktableList){
                assinfoWebList.add(new AssinfoWeb(checktable.getCheckinfo(),
                        checktable.getExpectinfo(),
                        checktable.getAutoExpectEnum()));
            }
            interinfoWeb.setAssinfoWebs(assinfoWebList);
            return interinfoWeb;
        }
        catch (Exception e){
            logger.error("--->>getIngerinfforid is system error;"+e);
        }
        return null;
    }

    private void setinterinfo(InterinfoWeb interinfoWeb,Interinfo interinfo){
        if(interinfoWeb == null || interinfo == null){
            return;
        }
        interinfo.setId(interinfoWeb.getInterid());
        interinfo.setCaseid(interinfoWeb.getCaseid());
        interinfo.setChecktype(interinfoWeb.getChecktype());
        interinfo.setHeadstr(getJsontoMap(interinfoWeb.getHeadstr()));
        interinfo.setHttpmeth(interinfoWeb.getHttpmeth());
        interinfo.setHttpparam(interinfoWeb.getHttpparam());
        interinfo.setHttptype(interinfoWeb.getHttptype());
        interinfo.setHttpurl(interinfoWeb.getHttpurl());
        interinfo.setPostparamtype(interinfoWeb.getPostparamtype());

        interinfo.setIntercasetype(interinfoWeb.getIntercasetype());
        interinfo.setInterfacename(interinfoWeb.getInterfacename());
    }

    private Checktable getchecktableinfo(AssinfoWeb assinfoWeb,int interinfoid){
        if(assinfoWeb == null){
            return null;
        }
        Checktable checktable=new Checktable();
        checktable.setInterid(interinfoid);
        checktable.setCheckinfo(assinfoWeb.getCheckinfo());
        checktable.setExpectinfo(assinfoWeb.getExpectinfo());
        checktable.setAutoExpectEnum(assinfoWeb.getAutotype());
        return checktable;
    }

    public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();
        map.put("a","a1");
        map.put("b","b1");
        System.out.println(JSON.toJSONString(map));
    }
}
